import Vue from "./Vue.js";

const vm = new Vue({
  el: "#app",
  beforeCreate() {
    console.log("beforeCreate");
  },
  mounted() {
    console.log("mounted");
  },
  updated() {
    console.log(this.num);
  },
  data: {
    name: "yahoo",
    content: `<span style="color:red">houdunren</span>`,
    title: "标题",
    num: 1,
    error: "",
    cart: [
      { id: 1, name: "IPAD", price: 100 },
      { id: 2, name: "IPHONE", price: 500 },
    ],
  },
  watch: {
    num(newValue, oldValue) {
      if (this.num <= 0) {
        this.num = 1;
        this.error = "数字不能小于1";
      } else {
        this.error = "";
      }
    },
  },
  computed: {
    total() {
      console.log(this.cart);
      return this.cart.reduce((t, g) => t + g.price, 0);
    },
    sum() {
      return this.num * 99;
    },
  },
  methods: {
    incr() {
      this.num++;
    },
    desc() {
      this.num--;
    },
  },
});

setInterval(() => {
  vm.cart[1].price += 10;
}, 500);
